package controler;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import logs.LogLevelEnum;
import logs.LogTypesEnum;
import view.ScreenManager;
import view.View;
import view.interfaces.ViewInterface;
import appli.Logger;
import controler.interfaces.ControlerInterface;
import controler.interfaces.MainControlerInterface;
import controler.interfaces.ModelControlerInterface;
import controler.interfaces.ViewControleurInterface;

public class AppliSIM implements ControlerInterface {

	private MainControlerInterface mainControler;
	private ViewControleurInterface viewControler;
	private ModelControlerInterface modelControler;
	private Logger logger;
	private String name;
	HashMap<String, File> availablesSimulations;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public AppliSIM(ModelControlerInterface modelControler,
			ViewControleurInterface viewControler,
			MainControlerInterface mainControler, Logger logger) {
		setName();
		this.modelControler = modelControler;
		this.viewControler = viewControler;
		this.mainControler = mainControler;

		this.logger = logger;

		Properties prop = new Properties();
		try {
			FileInputStream in = new FileInputStream("./simulations/sims.txt");
			prop.load(in);
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		availablesSimulations = new HashMap<String, File>();
		Set<Entry<Object, Object>> set = prop.entrySet();
		if (set != null) {
			Iterator<Entry<Object, Object>> it = set.iterator();
			while (it.hasNext()) {
				Entry<Object, Object> ent = it.next();
				String string = (String) ent.getKey();
				File file = new File((String) ent.getValue());
				availablesSimulations.put(string, file);
				System.out.println(availablesSimulations.toString());
			}
		}
		System.out.println(name.toString());
	}

	@Override
	public void init(ControlerInterface controler) {
		// update dependencies referencies
		initSubControlers();
		viewControler.initSpecificViewDatas(availablesSimulations);
		// MODEL

		// VIEW
		ScreenManager scr = new ScreenManager();
		ViewInterface view = new View(scr);
		viewControler.injectComponents(view);

		viewControler.start();

		// CONTROLER

	}

	private void initSubControlers() {

		// mainControler reports to this
		mainControler.init(this, modelControler, viewControler);

		// other controlers report to mainControler
		modelControler.init(mainControler);
		modelControler.initView(viewControler);

	}

	@Override
	public void log(LogLevelEnum logLevel, LogTypesEnum logType, String log) {
		logger.log(logLevel, logType, log);
	}

	@Override
	public void setName() {
		setName("Main_Application");
	}

	@Override
	public void quit() {
		logQuit();
		System.exit(0);
	}

	private void logQuit() {
		String log = " (" + this.getName() + ") " + "Quit";
		log(LogLevelEnum.INFO, LogTypesEnum.Appli, log);
	}

	@Override
	public void triggerAStep() {
		// TODO Auto-generated method stub
	}

}
